import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQBytesMessage;

import javax.jms.*;

/**
 * @author: blentle
 * @group: rd
 * @createdate: 2018/4/22 0022 下午 23:15
 * @mail: blentle.huan.ren@gmail.com
 * @description: quick  sort
 * @since: 1.0
 */
public class Publisher {

    private static String brokerUrl = "tcp://www.blentle.top:61616";
    private static final String TOPIC = "jack_mqtt_test";

    public void start() {
        // 消费者的主要流程
        Connection connection = null;
        try {
            // 1.初始化connection工厂
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

            // 2.创建Connection
            connection = connectionFactory.createConnection();


            // 3.打开连接
            connection.start();

            // 4.创建session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // 5.创建消息目标
            Destination destination = session.createTopic(TOPIC);

            // 6.创建消费者
            MessageProducer producer = session.createProducer(destination);
            while(true) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ActiveMQBytesMessage message = new ActiveMQBytesMessage();
//                message.writeByte((byte)20);
//                message.writeBytes("abcdefghjklm".getBytes());
//                message.writeByte((byte)80);
//                message.writeByte((byte)32);
//                message.writeByte((byte)0);
//                message.writeInt(0);
//                message.writeInt(1);
                message.writeByte((byte)0);
                //总长度
                message.writeShort((short)11);
                message.writeByte((byte)1);
                message.writeLong(System.currentTimeMillis());
                producer.send(message);
            }

        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Publisher().start();
    }
}
